import { message, Table, Input, Button, Popconfirm, Form, Select, Modal } from 'antd';
import { saveCityPromptMsg } from '@/services/business';
import styles from '../components.less';
import FormModal from './FormModal';
import { getValue, to } from '@/utils/smallTool';

const supportConfig = [
  { name: '不支持', value: '0' },
  { name: '官网地址', value: '1' },
  { name: '查悦地址', value: '2' },
]
const editConfig = [
  {
    name: 'cityCode',
    label: '城市编码',
    type: 'input',
    hidden: true
  },
  {
    name: 'type',
    label: '城市编码',
    type: 'input',
    hidden: true
  },
  {
    name: 'regType',
    label: '注册地址类型',
    type: 'radio',
    optionList: { list: supportConfig }
  },
  {
    name: 'regUrl',
    label: '注册地址',
    type: 'input'
  },
  {
    name: 'modType',
    label: '修改密码地址类型',
    type: 'radio',
    optionList: { list: supportConfig }
  },
  {
    name: 'modUrl',
    label: '修改密码地址',
    type: 'input'
  },
  {
    name: 'regInformation',
    label: '激活提示信息',
    type: 'editor',
  },
  {
    name: 'modInformation',
    label: '修改提示信息',
    type: 'editor',
  },
  {
    name: 'information',
    label: '绑卡提示信息',
    type: 'editor',
  },
]
function getMergeEditInput(config, data) {
  return config.map((item) => {
    item.initialValue = data[item.name] || '';
    return item;
  })
}
class PromptMessage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      sourcesData: []
    }
    this.$drawerForm = React.createRef();
  }
  componentWillReceiveProps(nextProps) {
    if (nextProps.sourcesdata.length !== this.state.sourcesData.length) {
      this.setState({
        sourcesData: nextProps.sourcesdata,
      })
    }
  }
  // 请求保存编辑的数据
  async saveEditor(validateRes) {
    let postRes = false;
    const dataObj = await saveCityPromptMsg(validateRes);
    if (dataObj && dataObj.code === '000000') {
      message.success('操作成功');
      postRes = true;
    } else {
      message.error('操作失败');
    }
    return postRes;
  }
  async handleSubmit() {
    const { getFieldDecorator, validateFields } = this.$drawerForm.current.props.form;
    let [validateError, validateRes] = await to(validateFields());
    if (validateError) return false;
    const postRes = await this.saveEditor(validateRes);
    if (postRes) {
      this.handleSave(validateRes);
    }
  };
  handleSave = (data) => {
    this.setState({ sourcesData: data });
  };
  render() {
    const { sourcesData } = this.state;
    const editInputConfig = getMergeEditInput(editConfig, sourcesData);
    return (
      <>
        <FormModal defaultData={editInputConfig} wrappedComponentRef={this.$drawerForm} />
        <div align="center">
          <Button onClick={this.handleSubmit.bind(this)} type="primary" >保存</Button>
        </div>
      </>
    );
  }
}
export default PromptMessage;